package net.sevear.chaos.download
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.utils.Timer;
	
	import net.sevear.chaos.event.SourceProxyEvent;
	import net.sevear.chaos.vo.TrackInfo;
	
	public class SourceProxy extends EventDispatcher
	{
		protected var timer:Timer;
		protected var curtrack:TrackInfo;
		protected var stepIdx:int;
		protected var loader:URLLoader;
		
		private var _type:int = -1;
		private var _enable:Boolean = true;
		
		public static const SEARCH_TIME:int = 10000;
		public function SourceProxy(type:int)
		{
			super();
			_type = type;
			
			timer = new Timer(SEARCH_TIME,1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerComplete);
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR,onIoError);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSecurityError);
		}
		
		public function get sourceType():int{
			return _type;
		}
		
		public function set enable(value:Boolean):void{
			_enable = value;
		}
		
		public function get enable():Boolean{
			return _enable;
		}
		
		public function searchSongNew(track:TrackInfo):void{
			
		}
		
		public function stopSearching():void{
			try{
				timer.stop();
				loader.close();
				curtrack = null;
			}catch(e:Error){
				
			}
		}
		
		private function onTimerComplete(e:TimerEvent):void{
			stopSearching();
			dispatchEvent(new SourceProxyEvent(SourceProxyEvent.TIME_OUT));
		}
		
		protected function downLoadTrackFile(e:SourceProxyEvent):void{
			
		}
		
		protected function onComplete(e:Event=null):void{
			
		}
		
		protected function onIoError(e:IOErrorEvent):void{
			dispatchEvent(new SourceProxyEvent(SourceProxyEvent.SERVICE_BREAK_DOWN));
		}
		
		protected function onSecurityError(e:SecurityErrorEvent):void{
			dispatchEvent(new SourceProxyEvent(SourceProxyEvent.SERVICE_BREAK_DOWN));
		}
	}
}